#include "ms575_algbackdatum.h"
using namespace MeasurE575;

AlgBackDatum::AlgBackDatum(int i):AlgBase(i)
{
}

void AlgBackDatum::operator()(cv::Mat &srcImg, int id)
{
    //Setup-1,Setup-2  33,34
    std::vector<cv::Point> contours0,contours1,contours2;

    {
        int i=33;
        std::vector<int> tparas;
        for(auto j=0;j< m_dataAllVec[i].m_edgeParam.cols;j++)
        {
            tparas.push_back(m_dataAllVec[i].m_edgeParam.at<int>(2,j));
        }
        //paras.push_back(tparas);
        std::vector<cv::Rect> rangVec;
        TemplateMatch(srcImg,m_dataAllVec[i],rangVec);
        cv::Mat imgt=srcImg(rangVec[0]).clone();
        edgedetect obj;
        obj.GetLineContours(imgt,tparas,contours0);
        ImageCoordinateTransformation(contours0,rangVec);
    }

    {
        int i=33;
        std::vector<int> tparas;
        for(auto j=0;j< m_dataAllVec[i].m_edgeParam.cols;j++)
        {
            tparas.push_back(m_dataAllVec[i].m_edgeParam.at<int>(3,j));
        }
        std::vector<cv::Rect> rangVec;
        TemplateMatch(srcImg,m_dataAllVec[i],rangVec);
        cv::Mat imgt=srcImg(rangVec[0]).clone();
        edgedetect obj;
        obj.GetLineContours(imgt,tparas,contours1);
        ImageCoordinateTransformation(contours1,rangVec);
    }
    {
        int i=34;
        std::vector<int> tparas;
        for(auto j=0;j< m_dataAllVec[i].m_edgeParam.cols;j++)
        {
            tparas.push_back(m_dataAllVec[i].m_edgeParam.at<int>(4,j));
        }
        std::vector<cv::Rect> rangVec;
        TemplateMatch(srcImg,m_dataAllVec[i],rangVec);
        cv::Mat imgt=srcImg(rangVec[0]).clone();
        edgedetect obj;
        obj.GetLineContours(imgt,tparas,contours2);
        ImageCoordinateTransformation(contours2,rangVec);
    }

    std::vector<cv::Point2f> xpts,ypts,datum;
    contours1.insert(contours1.end(),contours2.begin(),contours2.end());
//    for_each(contours3.begin(),contours3.end(),[](cv::Point &elem){
//        cv::Point2f tp(static_cast<float>(elem.x),static_cast<float>(elem.y));
//        xpts.push_back(tp);
//    });
//    for_each(contours0.begin(),contours0.end(),[](cv::Point &elem){
//        cv::Point2f tp(static_cast<float>(elem.x),static_cast<float>(elem.y));
//        ypts.push_back(tp);
//    });

    for(auto i=0;i<contours1.size();i++)
    {
        cv::Point2f tp(static_cast<float>(contours1[i].x),static_cast<float>(contours1[i].y));
        xpts.push_back(tp);
    }

    for(auto i=0;i<contours0.size();i++)
    {
        cv::Point2f tp(static_cast<float>(contours0[i].x),static_cast<float>(contours0[i].y));
        ypts.push_back(tp);
    }


    edgedetect obj;
    obj.FindDatumPoint(xpts, ypts, datum);

    AlgBase::datumBackVec[id]=datum;
    AlgBase::flagVec[id] = true;
    (*AlgBase::cvarptrVec[id]).notify_all();
}
